package dao.impl;

import dao.FinancingDao;
import entry.Financing;
import pojo.FinancingAndInstituty;
import pojo.FinancingCondition;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class FinancingDaoImpl implements FinancingDao {
    DBHelper dbHelper = new DBHelper();

    /**
     * insertFinancing 新增融资信息
     *
     * @param financing 要新增的融资信息
     * @return boolean 返回新增成功与否
     * @author 谈克思
     */
    @Override
    public boolean insertFinancing(Financing financing) {
        String sql="insert into financing values(?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

       return dbHelper.update(sql,financing.getId(),financing.getNo(),financing.getEnterprise_id(),financing.getAmount(),
       financing.getCost(),financing.getReceipt_amount(),financing.getInstituty_id(),
               financing.getCreate_time(),financing.getReceipt_time(),
               financing.getUplink_address(),financing.getStatus(),financing.getProgress_status(),
               financing.getPayment_interest_type(),financing.getRemark())!=0?true:false;
    }

    /**
     * selectFinancingCount 查找融资信息个数
     *
     * @param financingCondition 查询条件
     * @return int 返回有融资信息和金融机构信息的FinancingAndInstituty对象集合的元素个数
     * @author 金静远
     */
    @Override
    public int selectFinancingCount(FinancingCondition financingCondition) {
        StringBuffer sql = new StringBuffer("select count(financing.id) as id from financing INNER JOIN instituty ON financing.instituty_id = instituty.id where 1 = 1 ");
        ArrayList list = new ArrayList();
        if (financingCondition.getInstituty_id() != null) {
            sql.append(" and financing.enterprise_id = ? ");
            list.add(financingCondition.getInstituty_id());
        }
        if (financingCondition.getNo() != null) {
            sql.append(" and no = ? ");
            list.add(financingCondition.getNo());
        }
        if (financingCondition.getInstituty_id() != null) {
            sql.append(" and instituty.id = ? ");
            list.add(financingCondition.getInstituty_id());
        }
        if (financingCondition.getAmount_low() != null) {
            sql.append(" and ? <= amount ");
            list.add(financingCondition.getAmount_low());
        }
        if (financingCondition.getAmount_high() != null) {
            sql.append(" and ? >= amount ");
            list.add(financingCondition.getAmount_high());
        }
        if (financingCondition.getCreate_time() != null) {
            sql.append(" and create_time = ? ");
            list.add(financingCondition.getCreate_time());
        }
        if (financingCondition.getStatus() != null) {
            sql.append(" and status = ? ");
            list.add(financingCondition.getStatus());
        }
        return dbHelper.selectCount(sql.toString(), list.toArray());

    }

    /**
     * selectFinancingList 查找融资列表
     *
     * @param financingCondition 查询条件
     * @return ArrayList<FinancingAndInstituty> 返回有融资信息和金融机构信息的FinancingAndInstituty对象集合
     * @author 权明珠
     */
    @Override
    public ArrayList<FinancingAndInstituty> selectFinancingList(FinancingCondition financingCondition, PageHelper pageHelper) {
        StringBuffer sql = new StringBuffer("select financing.* ,instituty.name as instituty_name ,instituty.interest as instituty_interest, instituty.receipt_day as instituty_receipt_day from financing INNER JOIN instituty ON financing.instituty_id = instituty.id where 1 = 1 ");
        ArrayList list = new ArrayList();
        if (financingCondition.getInstituty_id() != null) {
            sql.append(" and financing.enterprise_id = ? ");
            list.add(financingCondition.getInstituty_id());
        }
        if (financingCondition.getNo() != null) {
            sql.append(" and no = ? ");
            list.add(financingCondition.getNo());
        }
        if (financingCondition.getInstituty_id() != null) {
            sql.append(" and instituty.id = ? ");
            list.add(financingCondition.getInstituty_id());
        }
        if (financingCondition.getAmount_low() != null) {
            sql.append(" and ? <= amount ");
            list.add(financingCondition.getAmount_low());
        }
        if (financingCondition.getAmount_high() != null) {
            sql.append(" and ? >= amount ");
            list.add(financingCondition.getAmount_high());
        }
        if (financingCondition.getCreate_time() != null) {
            sql.append(" and create_time = ? ");
            list.add(financingCondition.getCreate_time());
        }
        if (financingCondition.getStatus() != null) {
            sql.append(" and status = ? ");
            list.add(financingCondition.getStatus());
        }
        sql.append(" limit ?, ? ");
        int startIndex = (pageHelper.getWhichPage() - 1) * pageHelper.getPerPage();
        list.add(startIndex);
        list.add(pageHelper.getPerPage());
        return dbHelper.select(sql.toString(), FinancingAndInstituty.class, list.toArray());
    }

    /**
     * selectFinancingDetail 根据查找融资详情信息
     *
     * @param id Financing融资的id
     * @return FinancingAndInstituty 返回有融资信息和金融机构信息的FinancingAndInstituty对象
     * @author 权明珠
     */
    @Override
    public FinancingAndInstituty selectFinancingDetail(int id) {
        String sql = "select financing.* ,instituty.name as instituty_name ,instituty.interest as instituty_interest, instituty.receipt_day as instituty_receipt_day from financing INNER JOIN instituty ON financing.instituty_id = instituty.id where financing.id = ?";
        FinancingAndInstituty fin = (FinancingAndInstituty) (dbHelper.select(sql, FinancingAndInstituty.class, id).get(0));
        return fin;
    }
}

